package com.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Visitors;
import com.entity.Visitorsuser1;
import com.service.VisitorsService;
import com.service.Visitorsuser1Service;
import com.util.DataUtil;

@Controller
@RequestMapping("/visitors")
public class VisitorsController {
	
	private String url = "/home/apache-tomcat-7.0.47/webapps/image/wx"; //服务器存储图片地址
	DataUtil dataUtil=new DataUtil();
	
	@Resource
	VisitorsService visitorsService;
	@Resource
	Visitorsuser1Service visitorsuserService;
	
	/**
	 * 添加访客信息
	 * @param file
	 * @param request
	 * @param visitors
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value={"/insert"}, method=RequestMethod.POST)
	public String insert(@RequestParam(value="file", required=false) MultipartFile file, HttpServletRequest request,Visitors visitors){
	  String fileName = file.getOriginalFilename();
	  String yname=request.getParameter("yname");
	  String yphone=request.getParameter("yphone");
	  String ysex=request.getParameter("ysex");
	  String yaddress=request.getParameter("yaddress");
	  String yunitdoor=request.getParameter("yunitdoor");
	  String openid=request.getParameter("openid");
	  System.out.println(yname+"\t"+yphone+"\t"+ysex+"\t"+yaddress+"\t"+yunitdoor+"\t openid--"+openid);
	  String str = fileName.substring(fileName.indexOf("."));
	  Date data = new Date();
	  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss01");
	  String newData = dateFormat.format(data);
	  String newfileName = (newData + str);
	  File targetFile = new File(url, newfileName);
	  if (!targetFile.exists())
	    targetFile.mkdirs();
	  try
	  {
	    file.transferTo(targetFile);
	  } catch (Exception e) {
	    e.printStackTrace();
	  }
	  visitors.setYimageurl("https://wwl.ycws.cc/image/wx/"+newfileName);
	  visitors.setYname(yname);
	  visitors.setYphone(yphone);
	  visitors.setYsex(ysex);
	  visitors.setYaddress(yaddress);
	  visitors.setYcreatetime(dataUtil.data1());
	  visitors.setYupdatetime(dataUtil.data1());
	  visitors.setYunitdoor(yunitdoor);
	  visitors.setOpenid(openid);
	  visitors.setYindate("0");
	  int num=visitorsService.insert(visitors);
	  if(num==1){
		  Map<String,Object> maps=new HashMap<String,Object>();
		  maps.put("code", "1");
		  new JSONObject();
		  JSONObject josn=JSONObject.fromObject(maps);
		  return josn.toString();
	  }else{
		  Map<String,Object> maps=new HashMap<String,Object>();
		  maps.put("code", "0");
		  new JSONObject();
		  JSONObject josn=JSONObject.fromObject(maps);
		  return josn.toString();
	  }
	}
	/**
	 * 查询openid
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/openid",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	public String openid(String openid){
/*		String openid=request.getParameter("openid");
		System.out.println("openid"+openid);*/
		Visitors visitors=visitorsService.openid(openid);
		new JSONObject();
		JSONObject json=JSONObject.fromObject(visitors);
		System.out.println(json);
		return json.toString();
	}
	/**
	 * 查询授权人员信息
	 */
	@RequestMapping(value="/authorization",method=RequestMethod.GET)
	public String authorization(ModelMap map){
		List<Visitors> visitors=visitorsService.authorization();
		map.addAttribute("visitors", visitors);
		map.addAttribute("size", visitors.size());
		return "Visitors";
	}
}
